package org.web.naviray;

import org.web.naviray.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/* http://developer.android.com/resources/tutorials/views/hello-webview.html */
public class NaviRay extends Activity{
	
	private WebView browser;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        browser  = (WebView) findViewById(R.id.webview);
        browser.getSettings().setJavaScriptEnabled(true);

        browser.loadUrl("file:///android_asset/index.html");
        
        //browser.setWebViewClient(new NavRayClient());
        //browser.loadUrl("http://maps.google.com/maps?um=1&hl=en&biw=1019&bih=437&q=52+avenue+de+choisy+paris&ie=UTF-8&sa=N&tab=il");
    
    }
    
    /* Back History */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {
        	browser.goBack();
        	return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    private class NaviRayClient extends WebViewClient{
    	@Override
    	public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        view.loadUrl(url);
	        return true;
    	}
    }

    
    /* Options Menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.webmenu, menu);
    	return super.onCreateOptionsMenu(menu);
    }
    
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    	case R.id.menuAbout:
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setMessage(R.string.about_msg);
    		builder.setCancelable(false);
    		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();	
				}
			});
    	//	AlertDialog alert = builder.create();
    		
    		builder.show();
    		return true;
    	case R.id.menuHome:
            browser.loadUrl("file:///android_asset/index.html"); 
            //browser.setWebViewClient(new NavRayClient());
    		return true;
    	default:
    		return super.onOptionsItemSelected(item);
    	}
    }
}